﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel.Design;

namespace DataAccessor.Tools
{
    public delegate void StatementChangedEventHandler(UCStatement sender);
    public partial class UCStatement : UserControl
    {
        private bool commandMaxsizedFlag = false;
        private bool parametersGridMaxsizeFlag = false;

        private bool _miniMode = false;
        public bool MiniMode
        {
            get { return _miniMode; }
            set
            {
                _miniMode = value;
                if (_miniMode)
                {
                    this.labelTitle.Text = string.Format("Statement:{0}", this.txtName.Text);
                    this.picExpend.Image = global::DataAccessor.Tools.Properties.Resources._112_DownArrowLong_Blue_16x16_72;
                    this.panelEditor.Hide();
                    this.Size = new Size(this.Width, 30);
                }
                else
                {
                    this.labelTitle.Text = "Statement";
                    this.picExpend.Image = global::DataAccessor.Tools.Properties.Resources._112_UpArrowLong_Grey_16x16_72;
                    this.Size = new Size(this.Width, 520);
                    this.panelEditor.Show();
                }
            }
        }

        private TSQLStatement _statement;
        public TSQLStatement Statement
        {
            get
            {
                if (_statement == null)
                {
                    _statement = new TSQLStatement();
                    _statement.Command = new TSQLStatementCommand();
                    _statement.Params = new TSQLStatementParam[] { };
                }
                _statement.Name = txtName.Text;
                var commandType = CommandTypeRestriction.Text;
                Enum.TryParse<CommandTypeRestriction>(this.cmbCommandType.Text, out commandType);
                _statement.Command.Type = commandType;
                _statement.Command.Timeout = (int)this.numCommandTimeout.Value;
                _statement.Command.Value = this.richCommandText.Text;
                return _statement;
            }
            set
            {
                _statement = value;
                if (_statement != null)
                {
                    txtName.Text = _statement.Name;
                    cmbCommandType.Text = _statement.Command.Type.ToString();
                    numCommandTimeout.Value = _statement.Command.Timeout;
                    richCommandText.Text = _statement.Command.Value;
                    LoadParameters(_statement.Params);
                }
            }
        }

        public override ContextMenuStrip ContextMenuStrip
        {
            get
            {
                return base.ContextMenuStrip;
            }
            set
            {
                this.panelTitle.ContextMenuStrip = value;
                // base.ContextMenuStrip = value;
            }
        }

        #region events
        public event StatementChangedEventHandler StatementChanged;
        private void OnStatementChanged(UCStatement sender)
        {
            if (StatementChanged != null)
            {
                StatementChanged(sender);
            }
        }
        #endregion

        public UCStatement()
        {
            InitializeComponent();
            BindCommandType();
        }

        private void picExpend_Click(object sender, EventArgs e)
        {
            MiniMode = !MiniMode;
        }

        private void LoadParameters(TSQLStatementParam[] @params)
        {
            if (@params == null)
            {
                return;
            }
            foreach (var param in @params)
            {
                DataGridViewRow row = new DataGridViewRow();
                DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();
                cell.Value = param.Name;
                row.Cells.Add(cell);

                cell = new DataGridViewTextBoxCell();
                cell.Value = param.DataType;
                row.Cells.Add(cell);

                cell = new DataGridViewTextBoxCell();
                cell.Value = param.Length;
                row.Cells.Add(cell);

                cell = new DataGridViewTextBoxCell();
                cell.Value = param.Direction;
                row.Cells.Add(cell);

                row.HeaderCell.ContextMenuStrip = this.contextMenuStrip;
                row.Tag = param;
                gridParameters.Rows.Add(row);
            }
        }

        private void gridParameters_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            TSQLStatementParam param = new TSQLStatementParam();
            List<TSQLStatementParam> statementList = null;
            if (Statement.Params != null)
            {
                statementList = Statement.Params.ToList();
            }
            else
            {
                statementList = new List<TSQLStatementParam>();
            }
            statementList.Add(param);
            Statement.Params = statementList.ToArray();
            gridParameters.CurrentRow.Tag = param;

            DataGridViewImageCell cell = gridParameters.CurrentRow.Cells[colDelete.Name] as DataGridViewImageCell;
            cell.Value = global::DataAccessor.Tools.Properties.Resources.delete1;
        }

        private void gridParameters_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            var row = gridParameters.Rows[e.RowIndex];
            if (row.IsNewRow)
            {
                return;
            }
            var cell = row.Cells[e.ColumnIndex];
            var param = row.Tag as TSQLStatementParam;
            switch (e.ColumnIndex)
            {
                case 0:
                    var name = "";
                    if (cell.Value != null)
                    {
                        name = cell.Value.ToString();
                    }
                    param.Name = name;
                    break;
                case 1:
                    var dataType = ParameterDataTypeRestriction.AnsiString;
                    if (cell.Value != null)
                    {
                        Enum.TryParse<ParameterDataTypeRestriction>(cell.Value.ToString(), out dataType);
                    }
                    param.DataType = dataType;
                    break;
                case 2:
                    var length = 0;
                    if (cell.Value != null)
                    {
                        int.TryParse(cell.Value.ToString(), out length);
                    }
                    param.Length = length;
                    break;
                case 3:
                    var direction = ParameterDirectionRestriction.Input;
                    if (cell.Value != null)
                    {
                        Enum.TryParse<ParameterDirectionRestriction>(cell.Value.ToString(), out direction);
                    }
                    param.Direction = direction;
                    break;
            }
            OnStatementChanged(this);
        }

        private void gridParameters_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex < 0)
            {
                return;
            }

            DataGridViewColumn column = gridParameters.Columns[e.ColumnIndex];
            if (e.ColumnIndex != colDelete.Index)
            {
                return;
            }

            var row = gridParameters.Rows[e.RowIndex];

            if (row.IsNewRow)
            {
                return;
            }

            gridParameters.Rows.Remove(row);
            var param = row.Tag as TSQLStatementParam;
            var statementList = Statement.Params.ToList();
            statementList.Remove(param);
            Statement.Params = statementList.ToArray();

            row.Dispose();

            OnStatementChanged(this);
        }

        private void gridParameters_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
        {
        }

        private void BindCommandType()
        {
            cmbCommandType.DataSource = Enum.GetNames(typeof(DataAccessor.CommandTypeRestriction));
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            OnStatementChanged(this);
        }

        private void picMaxsizeCommand_Click(object sender, EventArgs e)
        {
            if (!commandMaxsizedFlag)
            {
                this.panelCommandTextContainer.Parent = this.panelEditor;
                this.panelCommandTextContainer.Dock = DockStyle.Fill;
                this.panelCommandTextContainer.BringToFront();
                this.picMaxsizeCommand.Image = global::DataAccessor.Tools.Properties.Resources.arrow_in;
            }
            else
            {
                this.panelCommandTextContainer.Parent = this.panelCommand;
                this.panelCommandTextContainer.Size = new Size(this.panelCommand.Width - 37, 137);
                this.panelCommandTextContainer.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
                this.panelCommandTextContainer.Dock = DockStyle.None;
                this.panelCommandTextContainer.Location = new Point(19, 55);
                this.picMaxsizeCommand.Image = global::DataAccessor.Tools.Properties.Resources.arrow_out;
            }
            commandMaxsizedFlag = !commandMaxsizedFlag;
        }

        private void picMaxsizeParametersGrid_Click(object sender, EventArgs e)
        {
            if (!parametersGridMaxsizeFlag)
            {
                this.panelCommandParametersContainer.Dock = DockStyle.Fill;
                this.panelCommandParametersContainer.BringToFront();
                this.picMaxsizeParametersGrid.Image = global::DataAccessor.Tools.Properties.Resources.arrow_in;
            }
            else
            {
                this.panelCommandParametersContainer.Size = new Size(this.panelEditor.Width - 78, 190);
                this.panelCommandParametersContainer.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
                this.panelCommandParametersContainer.Dock = DockStyle.None;
                this.panelCommandParametersContainer.Location = new Point(38, 281);
                this.picMaxsizeParametersGrid.Image = global::DataAccessor.Tools.Properties.Resources.arrow_out;
            }
            parametersGridMaxsizeFlag = !parametersGridMaxsizeFlag;
        }
    }
}
